﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Assets.DataSet;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Assets;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Helpers;
using System.IO;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class SchedulePrintForm : DevExpress.XtraEditors.XtraForm
    {
        public string _TourgroupID;

        public SchedulePrintForm(string tourgroupId)
        {
            InitializeComponent();

            _TourgroupID = tourgroupId;
        }

        private void btnPreviewAndPrint_Click(object sender, EventArgs e)
        {
            SimpleButton btn = sender as SimpleButton;

            ProviderDataSet travelAgentDs;
            ProviderDataSet hotelDs;
            StringBuilder travelNames, hotelNames;
            GetTravelAndHotelInfo(out travelAgentDs, out hotelDs, out travelNames,out hotelNames);
            DataTable dtTourgroup = TeamGroupCtrl.GetTeamGroupDataForSchedulePrint(_TourgroupID);
            string emailcontent = "";
            GuestListDataSet guestListDs = GetGuestListDataSet();
            GuestListFlightReport flightReport = new GuestListFlightReport();
            switch (bigClassGroup.SelectedIndex)
            {
                
                case 0://行程计划
                    DataTable dtSchedule = TeamGroupCtrl.GetTourGroupScheduleForSchedulePrint(_TourgroupID);
                    //获取计划行程数据，填充到打印数据集中
                    TourGroupScheduleDataSet scheduleDs = new TourGroupScheduleDataSet();
                    emailcontent = "大家好.请按照如下内容回复确认给我社\r\n\r\n团号:_________________\r\n抵达时间:______________ 离开时间:_____________________\r\n人数: _____________\r\n抵达交通工具:___________________ 离开交通工具: _______________________\r\n是否有全陪:______________________\r\n代订机车票: __________________\r\n代订房(房型+房间数):_____________________\r\n\r\n北京龙润国际旅行社有限公司";
                    foreach (DataRow dr in dtSchedule.Rows)
                    {
                        dr["Date"] = DateTime.Parse(dr["Date"].ToString()).ToString("yy-MM-dd ddd");
                        dr["Time"] = DateTime.Parse(dr["Time"].ToString()).ToString("HH:mm");
                        scheduleDs.Tables["TourGroupSchedule"].ImportRow(dr);
                    }
                    //获取对应团信息，填充到打印数据集中
                    foreach (DataRow dr in dtTourgroup.Rows)
                    {
                        scheduleDs.Tables["TourGroup"].ImportRow(dr);
                    }

                    TourGroupScheduleReport scheduleReport = new TourGroupScheduleReport(travelNames.ToString());
                    TravelAgentReport subRpt = new TravelAgentReport();
                    subRpt.DataSource = travelAgentDs;
                    scheduleReport.travelAgentSubreport.ReportSource = subRpt;
                    scheduleReport.DataSource = scheduleDs;
                    if (btn.Equals(btnPreview))
                        scheduleReport.ShowPreviewDialog();
                    if (btn.Equals(btnPrint))
                        scheduleReport.PrintDialog();
                    if (btn.Equals(btnEMail))
                    {
                        //生成行程计划文件并加入附件列表
                        string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "schedule_" + _TourgroupID.Trim() + ".pdf");
                        scheduleReport.ExportToPdf(file);

                        List<string> files = new List<string>();
                        files.Add(file);

                        //为EMail按钮生成客人名单
                        GuestListHotelReport guestHotelRpt = new GuestListHotelReport();
                        //TravelAgentReport travelSubRpt = new TravelAgentReport();
                        //HotelReport hotelSubRpt = new HotelReport();
                        //guestHotelRpt.travelSubReport.ReportSource = travelSubRpt;
                        //guestHotelRpt.hotelSubReport.ReportSource = hotelSubRpt;
                        guestHotelRpt.DataSource = guestListDs;
                        //travelSubRpt.DataSource = travelAgentDs;
                        //hotelSubRpt.DataSource = hotelDs;
                        
                        //生成客人名单文件并加入附件列表
                        file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "guests_" + _TourgroupID.Trim() + ".pdf");
                        guestHotelRpt.ExportToPdf(file);
                        files.Add(file);

                        //为EMail按钮生成飞机名单
                        flightReport.DataSource = guestListDs;
                        file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "flight_" + _TourgroupID.Trim() + ".pdf");
                        flightReport.ExportToPdf(file);
                        files.Add(file);

                        //发送邮件
                        SendMail(files, dtTourgroup.Rows[0]["TourgroupName"] + "行程计划", emailcontent);
                        
                        //删除附件
                        foreach (string filePath in files)
                        {
                            if (File.Exists(filePath))
                                File.Delete(filePath);
                        }
                    }
                    break;
                case 1://订房单
                    //宾馆备注信息
                    string remark = "";
                    RoomRemarkForm remarkFom = new RoomRemarkForm(_TourgroupID);
                    if (remarkFom.ShowDialog(this) == DialogResult.OK)
                        remark = remarkFom.mbRoomRemark.Text;
                    else//没点击‘确定’按钮，则不打印
                        return;
                    TeamGroupCtrl.GetTeamGroupRoomRemark(_TourgroupID);

                    emailcontent = "大家好.请按照如下内容回复确认给我社\r\n\r\n团号: _____________________\r\n用房数: _____________________\r\n房型:_________________________\r\n时间起止:_________________________\r\n陪同床:___________________________\r\n早餐:___________________________\r\n抵达日早餐是否有:_______________________\r\n\r\n北京龙润国际旅行社有限公司";
                    
                    HotelOrderDataSet hotelOrderDs = new HotelOrderDataSet();
                    DataTable hotelOrder = HotelOrderCtrl.GetHotelOrderForPrint(_TourgroupID);
                    foreach (DataRow dr in hotelOrder.Rows)
                    {
                        hotelOrderDs.Tables["HotelOrder"].ImportRow(dr);
                    }
                    HotelOrderReport hotelReport = new HotelOrderReport(_TourgroupID, remark, hotelNames.ToString());
                    HotelReport hotelSubReport = new HotelReport();
                    hotelSubReport.DataSource = hotelDs;
                    hotelReport.HotelSubreport.ReportSource = hotelSubReport;
                    hotelReport.DataSource = hotelOrderDs;
                    if (btn.Equals(btnPreview))
                        hotelReport.ShowPreviewDialog();
                    if (btn.Equals(btnPrint))
                        hotelReport.PrintDialog();
                    if (btn.Equals(btnEMail))
                    {
                        //生成订房单文件并加入附件列表
                        string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hotel_" + _TourgroupID.Trim() + ".pdf");
                        hotelReport.ExportToPdf(file);
                        List<string> files = new List<string>();
                        files.Add(file);

                        //生成客人名单
                        GuestListHotelReport guestHotelRpt = new GuestListHotelReport();
                        //TravelAgentReport travelSubRpt = new TravelAgentReport();
                        //HotelReport hotelSubRpt = new HotelReport();
                        //guestHotelRpt.travelSubReport.ReportSource = travelSubRpt;
                        //guestHotelRpt.hotelSubReport.ReportSource = hotelSubRpt;
                        guestHotelRpt.DataSource = guestListDs;
                        //travelSubRpt.DataSource = travelAgentDs;
                        //hotelSubRpt.DataSource = hotelDs;

                        //生成客人名单文件并加入附件列表
                        file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "guests_" + _TourgroupID.Trim() + ".pdf");
                        guestHotelRpt.ExportToPdf(file);
                        files.Add(file);

                        //为EMail按钮生成飞机名单
                        flightReport.DataSource = guestListDs;
                        file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "flight_" + _TourgroupID.Trim() + ".pdf");
                        flightReport.ExportToPdf(file);
                        files.Add(file);

                        SendMail(files, dtTourgroup.Rows[0]["TourgroupName"] + "订房单", emailcontent);
                        foreach (string filePath in files)
                        {
                            if (File.Exists(filePath))
                                File.Delete(filePath);
                        }

                    }
                    break;
                case 2://飞机名单
                    emailcontent = "请收到此通知后回复确认。\r\n\r\n北京龙润国际旅行社有限公司";                   
                    flightReport.DataSource = guestListDs;
                    if (btn.Equals(btnPreview))
                        flightReport.ShowPreviewDialog();
                    if (btn.Equals(btnPrint))
                        flightReport.PrintDialog();
                    if (btn.Equals(btnEMail))
                    {
                        string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "flight_" + _TourgroupID.Trim() + ".pdf");
                        flightReport.ExportToPdf(file);
                        List<string> files = new List<string>();
                        files.Add(file);
                        //生成客人名单
                        GuestListHotelReport guestHotelRpt1 = new GuestListHotelReport();
                        guestHotelRpt1.DataSource = guestListDs;

                        //生成客人名单文件并加入附件列表
                        file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "guests_" + _TourgroupID.Trim() + ".pdf");
                        guestHotelRpt1.ExportToPdf(file);
                        files.Add(file);

                        //生成客人名单
                        GuestListHotelReport guestHotelRpt = new GuestListHotelReport();
                        //TravelAgentReport travelSubRpt = new TravelAgentReport();
                        //HotelReport hotelSubRpt = new HotelReport();
                        //guestHotelRpt.travelSubReport.ReportSource = travelSubRpt;
                        //guestHotelRpt.hotelSubReport.ReportSource = hotelSubRpt;
                        guestHotelRpt.DataSource = guestListDs;
                        //travelSubRpt.DataSource = travelAgentDs;
                        //hotelSubRpt.DataSource = hotelDs;

                        //生成客人名单文件并加入附件列表
                        file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "guests_" + _TourgroupID.Trim() + ".pdf");
                        guestHotelRpt.ExportToPdf(file);
                        files.Add(file);

                        //为EMail按钮生成飞机名单
                        flightReport.DataSource = guestListDs;
                        file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "flight_" + _TourgroupID.Trim() + ".pdf");
                        flightReport.ExportToPdf(file);
                        files.Add(file);

                        SendMail(files, dtTourgroup.Rows[0]["TourgroupName"] + "飞机名单", emailcontent);
                        foreach (string filePath in files)
                        {
                            if (File.Exists(filePath))
                                File.Delete(filePath);
                        }

                    }
                    break;
                case 3://酒店名单
                    emailcontent = "请收到此通知后回复确认。\r\n\r\n北京龙润国际旅行社有限公司";
                    HotelListReport hotelListRpt = new HotelListReport(_TourgroupID);
                    hotelListRpt.DataSource = hotelDs;
                    if (btn.Equals(btnPreview))
                        hotelListRpt.ShowPreviewDialog();
                    if (btn.Equals(btnPrint))
                        hotelListRpt.PrintDialog();
                    if (btn.Equals(btnEMail))
                    {
                        string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hotelList_" + _TourgroupID.Trim() + ".pdf");
                        hotelListRpt.ExportToPdf(file);
                        List<string> files = new List<string>();
                        files.Add(file);

                        SendMail(files, dtTourgroup.Rows[0]["TourgroupName"] + "酒店List", emailcontent);
                        foreach (string filePath in files)
                        {
                            if (File.Exists(filePath))
                                File.Delete(filePath);
                        }

                    }
                    break;
                default:
                    break;
            }
        }

        private void GetTravelAndHotelInfo(out ProviderDataSet travelAgentDs, out ProviderDataSet hotelDs, out StringBuilder travelNames, out StringBuilder hotelNames)
        {
            travelAgentDs = new ProviderDataSet();
            hotelDs = new ProviderDataSet();

            //获取行程中的旅行社ID，好奇怪啊，为什么要先获得ID，再获得旅行社信息？一个查询不就出来了？
            travelNames = new StringBuilder();
            hotelNames = new StringBuilder();
            //DataTable dtProviderIds = TeamGroupCtrl.GetScheduleTravelsAndHotelIDForPrint(_TourgroupID);
            DataTable dtTravelIds = TeamGroupCtrl.GetScheduleTravelsIDForPrint(_TourgroupID);
            DataTable dtHotelIds = TeamGroupCtrl.GetScheduleHotelIDForPrint(_TourgroupID);

            string[] travelIds = new String[dtTravelIds.Rows.Count];
            string[] hotelIds = new string[dtHotelIds.Rows.Count];
            for (int i = 0; i < dtTravelIds.Rows.Count; i++)
            {
                //存储旅行社ID
                travelIds[i] = dtTravelIds.Rows[i]["TravelAgencyID"].ToString();
            }
            for (int i = 0; i < dtHotelIds.Rows.Count; i++)
            {
                //存储宾馆ID
                hotelIds[i] = dtHotelIds.Rows[i]["HotelID"].ToString();
            }

            //获取旅行社信息，填充到打印数据集中
            DataTable dtTravelInfo = TeamGroupCtrl.GetTravelOrHotelInfoForPrint(travelIds);
            for (int i = 0; i < dtTravelInfo.Rows.Count; i++)
            {
                //存储旅行社Name
                if (i == dtTravelIds.Rows.Count)
                    travelNames.AppendFormat(dtTravelInfo.Rows[i]["ProviderName"].ToString());
                else
                    travelNames.AppendFormat("{0} ", dtTravelInfo.Rows[i]["ProviderName"].ToString());

                travelAgentDs.Tables["Provider"].ImportRow(dtTravelInfo.Rows[i]);
            }
            //获取宾馆信息，填充到打印数据集中
            DataTable dtHotelInfo = TeamGroupCtrl.GetTravelOrHotelInfoForPrint(hotelIds);
            for (int i = 0; i < dtHotelInfo.Rows.Count; i++)
            {
                //存储宾馆Name
                if (i == dtHotelIds.Rows.Count)
                    hotelNames.AppendFormat(dtHotelInfo.Rows[i]["ProviderName"].ToString());
                else
                    hotelNames.AppendFormat("{0} ", dtHotelInfo.Rows[i]["ProviderName"].ToString());

                hotelDs.Tables["Provider"].ImportRow(dtHotelInfo.Rows[i]);
            }
        }
        private GuestListDataSet GetGuestListDataSet()
        {
            GuestListDataSet guestListDs = new GuestListDataSet();
            DataTable guest = TeamGroupCtrl.GetGuestListData(_TourgroupID);
            foreach (DataRow dr in guest.Rows)
                guestListDs.Tables["GuestList"].ImportRow(dr);
            //团数据
            DataTable tourgroup = TeamGroupCtrl.GetTeamGroupDataForSchedulePrint(_TourgroupID);
            foreach (DataRow dr in tourgroup.Rows)
                guestListDs.Tables["TourGroup"].ImportRow(dr);
            return guestListDs;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void SendMail(List<string> files, string title, string content)
        {
            SendMailForm mailForm = new SendMailForm(_TourgroupID);
            if (mailForm.ShowDialog(this) == DialogResult.OK)
            {
                if (mailForm._ReturnMails != null && mailForm._ReturnMails.Count > 0)
                {
                    string departmentId = UserInfoCtrl.GetUserInfoForDepartmentID(Thread.CurrentPrincipal.Identity.Name);
                    
                    //在这里判定是否附附加客人名单或飞机名单
                    if (mailForm.tbSendNone.Checked)
                    {
                        files.RemoveRange(1,2);
                    }
                    else
                    {
                        if (!mailForm.tbSendGuestList.Checked)
                            files.RemoveAt(1);
                        if (!mailForm.tbSendPlaneList.Checked)
                            files.RemoveAt(2);
                    }
                    if (!string.IsNullOrEmpty(departmentId))
                    {
                        DataTable dtDepartment = BaseInfoCtrl.GetDepartmentDataByDepartmentID(int.Parse(departmentId));
                        if (EMailHelper.SendMail(mailForm._ReturnMails, title, content, files, TeamGroupCtrl.GetDepartmentEmailById(_TourgroupID), mailForm._ReturnRecipients, _TourgroupID, TeamGroupCtrl.GetTeamGroupNameById(_TourgroupID), LogTypeEnum.操作类))
                            MessageBox.Show("邮件发送成功！", "信息提示");
                        else
                            MessageBox.Show("邮件发送失败，详情请查看日志", "信息提示");
                    }
                    else
                    {
                        MessageBox.Show("部门邮件设置有误，邮件发送失败！", "信息提示");
                    }
                }
            }
        }
    }
}
